package com.san.test;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;

import com.san.dissertation.bpm.model.Process;
import com.san.dissertation.bpm.model.ProcessInstance;
import com.san.dissertation.dao.service.impl.DAOServiceImpl;
import com.san.simulation.model.impl.ParseProcess;


public class BusinessService {

	private Launcher launcher;
	private ParseProcess parseProcess;
	private DAOServiceImpl daoServiceImpl;
	
	
	public void initiateProcess(InputStream inputStream, String name) {

		RepositoryService repositoryService = launcher.getRepositoryService();
		RuntimeService runtimeService = launcher.getRuntimeService();
		TaskService taskService = launcher.getTaskService();

		// repositoryService.createDeployment().addClasspathResource("makercheckerRevised.bpmn20.xml").deploy();

		repositoryService.createDeployment().addInputStream(name, inputStream)
				.deploy();

		Map<String, Object> variables = new HashMap<String, Object>();
		variables.put("status", "START");

		org.activiti.engine.runtime.ProcessInstance processInstance = runtimeService
				.startProcessInstanceByKey("sankarMakerChecker", variables);

		System.out.println(processInstance.getProcessInstanceId());

		Task task = taskService.createTaskQuery()
				.processInstanceId(processInstance.getProcessInstanceId())
				.singleResult();

		taskService.claim(task.getId(), "sankar-maker");

		variables.put("status", "MAKER");
		taskService.complete(task.getId(), variables);

		task = taskService.createTaskQuery()
				.processInstanceId(processInstance.getProcessInstanceId())
				.singleResult();

		taskService.claim(task.getId(), "sankar-checker");

		variables.put("status", "Authorized");
		variables.put("isCompleted", "true");

		taskService.complete(task.getId(), variables);

	}

	public List<ProcessInstance> getProcessInstanceDetails(String processKey) {
			
		Process process= daoServiceImpl.getProcessRepository().findByKey(processKey);
		
		if(process==null){
			
			return null;
		}
		
		 List<ProcessInstance> processInstances=daoServiceImpl.getProcessInstanceRepository().getProcessInstanceByProcess(process);
		
		return processInstances;
	}
	
	public List<Process> getAllProcessDetails() {
		
		List<Process> process= (List<Process>) daoServiceImpl.getProcessRepository().findAll();
		
		return process;
	}
	

	public Launcher getLauncher() {
		return launcher;
	}

	public void setLauncher(Launcher launcher) {
		this.launcher = launcher;
	}

	public ParseProcess getParseProcess() {
		return parseProcess;
	}

	public void setParseProcess(ParseProcess parseProcess) {
		this.parseProcess = parseProcess;
	}

	public DAOServiceImpl getDaoServiceImpl() {
		return daoServiceImpl;
	}

	public void setDaoServiceImpl(DAOServiceImpl daoServiceImpl) {
		this.daoServiceImpl = daoServiceImpl;
	}
	
	

}
